package com.bw.ivideo.testmvi.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.bw.ivideo.R
import com.bw.ivideo.testmvi.intent.VideoIntent
import com.bw.ivideo.testmvi.state.VideoState
import com.bw.ivideo.testmvi.viewmodel.VideoViewModel
import com.bw.lib_mvicore.ui.BaseMviActivity
import kotlinx.coroutines.launch

class VideoActivity : BaseMviActivity() {
    var videoViewModel: VideoViewModel? = null
    override fun generateViewModel() {
        videoViewModel = ViewModelProvider(this).get(VideoViewModel::class.java)
    }

    override fun initData() {
        super.initData()
        //发送意图
        lifecycleScope.launch{
            repeatOnLifecycle(Lifecycle.State.STARTED){
                videoViewModel?.intents?.send(VideoIntent.GetVideoIntent(1,10))
            }
        }
    }

    override fun getLayoutView(): View {
        return null!!
    }

    override fun getLayoutId(layoutId: Int): Int {
        return R.layout.activity_video
    }
    //处理状态
    override fun handleState() {
        lifecycleScope.launch{
            repeatOnLifecycle(Lifecycle.State.STARTED){
                videoViewModel?.state?.collect{
                    when(it){
                        is VideoState.Loading->{
                            showMsg("数据加载中 ")
                        }
                        is VideoState.Success->{
                            showMsg("数据加载成功 ${it.data}")
                        }
                        is VideoState.Error->{
                            showMsg("数据加载失败 ${it.error}")
                        }
                        else -> {}
                    }
                }
            }
        }
    }
}